<?php


namespace App\Http\Controllers\Index;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use ReCaptcha\Response;

class AiController extends Controller
{
    private $Place = [
        'New York', 'Los Angeles', 'San Francisco', 'Vancouver', 'Ottawa', 'Toronto', 'Sydney', 'Melbourne', 'Tokyo', 'Kobe', 'Yokohama', 'San Jose', 'Seattle', 'Boston'
    ];
    private $NickName = [
        'Greg', 'Hardy', 'Harriet', 'Gilbert', 'Giles', 'Glenn', 'Goddard', 'Godfery', 'Gordon', 'Griffith', 'Grover', 'Gustave', 'Guy', 'Hale', 'Haley', 'Hamiltion', 'Hardy', 'Harlan', 'Harley', 'Harold', 'Kennedy', 'Kenh', 'Kent', 'Kerwin', 'Levi', 'Lewis', 'Lyndon', 'Martin', 'Merle', 'Rickard', 'bobby', 'charles', 'christina', 'Nathan', 'Nicholas', 'stephan', 'Milo', 'Monroe', 'Montague', 'Morgan', 'Nicholas', 'Nigel', 'Noah', 'Noel', 'Osborn', 'Otis', 'Otto', 'Owen', 'Parker', 'Patrick'
    ];
    private $NickPre = [
      'Geoff', 'Rick', 'Bob Rob', 'Chuck', 'Chris', 'Nat', 'Nick', 'Steve', 'Little', 'Cute', 'Carter', 'Smith', 'Longfellow', 'Black', 'Rice', 'Bird', 'Burne-Jones', 'Miller', 'Ronald'
    ];

    /**
     * 返回生成的用户购买记录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function simulation(Request $request){
        $page = $request->get('page');
        $arr = array();
        for ($i = 0; $i < 20; $i++){
            $product = $this->createProduct();
            $product->place = $this->createCity();
            $product->nick_name = $this->getNickName();
            $product->purchase_at = $this->createTime($page);
            $arr[] = $product;
        }
        return JsonResponse(200,true,'获取成功', $arr);
    }

    /**
     * 拼接用户名
     * @return string
     */
    private function getNickName(){
        $pre_index = rand(0,count($this->NickPre)-1);
        $suf_index = rand(0,count($this->NickName)-1);
        return $this->NickPre[$pre_index].' '.$this->NickName[$suf_index];
    }

    /**
     * 生成用户所在地
     * @return mixed
     */
    private function createCity(){
        $index = rand(0,count($this->Place) - 1);
        return $this->Place[$index];
    }

    private function createProduct(){
        $product = DB::table('product')
            ->get();
        $index = rand(0,count($product)-1);
        return $product[$index];
    }

    private function createTime($page){
        if ($page > 1){
            $start = time() - 3600 * 24 * $page;
            $end = time() - 3600 * 24 * ($page - 1);
        }else{
            $start = time() - 3600 * 24 * $page;
            $end = time();
        }
        return date('Y-m-d H:i:s',rand($start, $end));
    }
}
